<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="90px">
      <!--      <el-form-item label="类型" prop="accountType">-->
      <!--        <el-select v-model="queryParams.accountType" placeholder="请选择类型" clearable>-->
      <!--&lt;!&ndash;          <el-option label="支付宝" :value="1" />&ndash;&gt;-->
      <!--          <el-option label="银行卡" :value="2" />-->
      <!--        </el-select>-->
      <!--      </el-form-item>-->
      <el-form-item label="收款类型" prop="backType">
        <el-select v-model="queryParams.backType" placeholder="请选择收款类型" clearable>
          <el-option label="个人" :value="1" />
          <el-option label="对公" :value="2" />
        </el-select>
      </el-form-item>
      <el-form-item label="银行开户名" prop="name" label-width="130px">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入银行开户名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="银行卡号" prop="cardNo" label-width="130px">
        <el-input
          v-model="queryParams.cardNo"
          placeholder="请输入银行卡号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="银行名称" prop="backName">
        <el-input
          v-model="queryParams.backName"
          placeholder="请输入银行名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['withdrawal:withdrawalAccount:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['withdrawal:withdrawalAccount:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="withdrawalAccountList" @selection-change="handleSelectionChange">
      <!--      <el-table-column label="类型 " align="center" prop="accountType">-->
      <!--        <template slot-scope="{ row }">-->
      <!--          <el-tag v-if="row.accountType === 1" type="success">支付宝</el-tag>-->
      <!--          <el-tag v-else-if="row.accountType === 2" >银行卡</el-tag>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <el-table-column label="收款类型" align="center" prop="backType">
        <template slot-scope="{ row }">
          <el-tag v-if="row.backType === 1" type="success">个人</el-tag>
          <el-tag v-else-if="row.backType === 2" >对公</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="银行名称" align="center" prop="backName" />
      <el-table-column label="银行开户名" align="center" prop="name" />
      <el-table-column label="银行开户名" align="center" prop="name" />
      <el-table-column label="银行开户支行" align="center" prop="cardBank" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['withdrawal:withdrawalAccount:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['withdrawal:withdrawalAccount:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改提现账号对话框 -->
    <el-dialog :close-on-click-modal="false" :close-on-press-escape="false" :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <!--        <el-form-item label="类型" prop="accountType">-->
        <!--          <el-select v-model="form.accountType" placeholder="请选择类型" clearable>-->
        <!--            <el-option label="银行卡" :value="2" />-->
        <!--          </el-select>-->
        <!--        </el-form-item>-->
        <el-form-item label="收款类型" v-if="form.accountType === 2" prop="backType">
          <el-select v-model="form.backType" placeholder="请选择收款类型" clearable>
            <el-option label="个人" :value="1" />
            <el-option label="对公" :value="2" />
          </el-select>
        </el-form-item>
        <el-form-item label="银行名称" prop="backName">
          <el-select remote v-model="form.backName" reserve-keyword filterable placeholder="请输入银行名称" :remote-method="remoteMethod">
            <el-option
              v-for="(item,index) in back"
              :key="index"
              :label="item.name"
              :value="item.name">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="银行开户名" prop="name">
          <el-input v-model="form.name" placeholder="请输入银行开户名" />
        </el-form-item>
        <el-form-item label="银行开户支行" prop="cardBank">
          <el-input v-model="form.cardBank" placeholder="请输入银行开户支行" />
        </el-form-item>
        <el-form-item label="银行卡号" prop="cardNo">
          <el-input v-model="form.cardNo" placeholder="请输入银行卡号" />
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listWithdrawalAccount, getWithdrawalAccount, delWithdrawalAccount, addWithdrawalAccount, updateWithdrawalAccount } from "@/api/withdrawal/withdrawalAccount";
import { listAliBank } from '@/api/system/aliBank'
import store from '@/store'

export default {
  name: "WithdrawalAccount",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      back:[],
      // 总条数
      total: 0,
      // 提现账号表格数据
      withdrawalAccountList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        tslId:null,
        accountType: null,
        backType: null,
        name: null,
        cardNo: null,
        backName: null,
      },
      roleGroup: store.getters.roles,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        accountType: [
          { required: true, message: "请选择类型", trigger: ["blur","change"] }
        ],
        backType: [
          { required: true, message: "请选择收款类型", trigger: ["blur","change"] }
        ],
        name: [
          { required: true, message: "银行开户名不能为空", trigger: ["blur","change"] }
        ],
        cardNo: [
          { required: true, message: "银行卡号不能为空", trigger: ["blur","change"] }
        ],
        cardBank: [
          { required: true, message: "银行开户支行不能为空", trigger: ["blur","change"] }
        ],
        backName: [
          { required: true, message: "银行名称不能为空", trigger: ["blur","change"] }
        ],
      }
    };
  },
  created() {

    if (this.roleGroup.includes('service_provider')) {
      let tslData = JSON.parse(localStorage.getItem('tslData') || '{}');
      if (tslData) {
        this.queryParams.tslId = tslData.id
      }
    }
    this.getList();
  },
  methods: {
    /** 查询提现账号列表 */
    getList() {
      this.loading = true;
      listWithdrawalAccount(this.queryParams).then(response => {
        this.withdrawalAccountList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        accountType: 2,
        backType: null,
        name: null,
        cardNo: null,
        cardBank:null,
        backName: null,
        createTime: null,
        updateTime: null,
        tslId:this.queryParams.tslId
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加提现账号";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getWithdrawalAccount(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改提现账号";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateWithdrawalAccount(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addWithdrawalAccount(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal.confirm('是否确认删除提现账号为"' + row.name + '"的数据项？').then(function() {
        return delWithdrawalAccount(row.id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    remoteMethod(query){
      if (query !== ""){
        listAliBank({name:query,type:this.form.backType}).then(response => {
          this.back = response.rows
        })
      } else {
        this.back = [];
      }
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('ecs/withdrawalAccount/export', {
        ...this.queryParams
      }, `提现账号_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
